//
//  NSDictionary+EV.m
//  EVClub
//
//  Created by sam on 14-8-28.
//  Copyright (c) 2014年 BitRice. All rights reserved.
//

#import "NSDictionary+EV.h"
#import <objc/runtime.h>

@implementation NSDictionary (EV)
- (id)ev_safeObjectForKey:(id)aKey
{
    id obj = [self objectForKey:aKey];
    if ([obj isKindOfClass:[NSNull class]]) {
        return @"";
    }
    if (!obj) {
        return @"";
    }
    return obj;
}

- (id)ev_nilObjectForKey:(id)aKey
{
    id obj = [self objectForKey:aKey];
    if ([obj isKindOfClass:[NSNull class]]) {
        return nil;
    }
    if (!obj) {
        return nil;
    }
    return obj;
}

- (id)ev_nilObjectForKey:(id)aKey forClass:(Class)objectClass {
    id object = [self ev_nilObjectForKey:aKey];
    return [object isKindOfClass:objectClass] ? object : nil;
}

- (NSString *)ev_stringObjectForKey:(id)aKey
{
    id obj = [self ev_safeObjectForKey:aKey];
    return [NSString stringWithFormat:@"%@",obj];
}

- (NSInteger)ev_integerForKey:(id)aKey {
    return [self ev_integerForKey:aKey withDefault:0];
}

- (NSInteger)ev_integerForKey:(id)aKey withDefault:(NSInteger)defaultValue {
    if ([self.allKeys containsObject:aKey]) {
        return [[self ev_stringObjectForKey:aKey] integerValue];
    }
    else {
        return defaultValue;
    }
}

- (double)ev_doubleForKey:(id)aKey {
    return [[self ev_stringObjectForKey:aKey] doubleValue];
}

- (NSDate *)ev_dateObjectForKey:(id)aKey {
    return [NSDate dateWithTimeIntervalSince1970:[self ev_doubleForKey:aKey]];
}

- (BOOL)ev_boolForKey:(id)aKey {
    return [self ev_integerForKey:aKey] > 0;
}

- (BOOL)ev_boolForKey:(id)aKey withDefault:(BOOL)defaultValue {
    return [self ev_integerForKey:aKey withDefault:defaultValue ? 1 : 0] > 0;
}

- (NSArray *)ev_arrayObjectForKey:(id)aKey {
    return [self ev_nilObjectForKey:aKey forClass:[NSArray class]];
}

- (NSDictionary *)ev_dictionaryObjectForKey:(id)aKey {
    return [self ev_nilObjectForKey:aKey forClass:[NSDictionary class]];
}

- (id)ev_objectForKey:(id)aKey withDefault:(id)defaultValue {
    if (self[aKey] != nil && ![self[aKey] isKindOfClass:[NSNull class]]) {
        return [self objectWithoutNSNumber:self[aKey]];
    } else {
        return defaultValue;
    }
}

- (id)ev_objectForKeyArray:(NSArray*)keyArray withDefault:(id)defaultValue {
    for (id aKey in keyArray) {
        if ([self.allKeys containsObject:aKey] && ![self[aKey] isKindOfClass:[NSNull class]]) {
            return [self objectWithoutNSNumber:self[aKey]];
        }
    }
    return defaultValue;
}

- (id)objectWithoutNSNumber:(id)object {
    if ([object isKindOfClass:[NSNumber class]]) {
        return [NSString stringWithFormat:@"%@", object];
    }
    return object;
}

@end


@implementation NSDictionary (EVNetWork)

- (NSString *)ev_errorMessage {
    return [self ev_stringObjectForKey:@"error"];
}

- (NSInteger)ev_errorCode {
    return [self ev_integerForKey:@"code" withDefault:1];
}

- (NSDictionary *)ev_responseData {
    return [self ev_dictionaryObjectForKey:@"data"];
}
@end
